<template>
  <div class="site-settings">
    <div class="page-header">
      <h1 class="page-title">网站设置</h1>
      <div class="page-actions">
        <button class="save-btn primary-btn" @click="saveSettings" :disabled="isSaving">
          <i class="fas fa-save"></i> 保存设置
        </button>
      </div>
    </div>

    <div class="settings-tabs">
      <button 
        v-for="tab in tabs" 
        :key="tab.id" 
        class="tab-btn" 
        :class="{ 'active': activeTab === tab.id }"
        @click="activeTab = tab.id"
      >
        <i :class="tab.icon"></i>
        <span>{{ tab.name }}</span>
      </button>
    </div>

    <div class="settings-content">
      <!-- 基本设置 -->
      <div v-if="activeTab === 'basic'" class="settings-section">
        <div class="section-header">
          <h2>基本设置</h2>
          <p>配置网站的基本信息</p>
        </div>
        
        <div class="settings-form">
          <div class="form-group">
            <label for="site-title">网站标题 <span class="required">*</span></label>
            <input 
              type="text" 
              id="site-title" 
              v-model="settings.siteTitle" 
              required
              placeholder="输入网站标题"
            >
          </div>
          
          <div class="form-group">
            <label for="site-description">网站描述</label>
            <textarea 
              id="site-description" 
              v-model="settings.siteDescription" 
              rows="3"
              placeholder="简短描述您的网站"
            ></textarea>
          </div>
          
          <div class="form-row">
            <div class="form-group">
              <label for="site-logo">网站Logo URL</label>
              <input 
                type="url" 
                id="site-logo" 
                v-model="settings.siteLogo" 
                placeholder="输入Logo图片URL"
              >
              <div v-if="settings.siteLogo" class="image-preview small">
                <img :src="settings.siteLogo" alt="网站Logo预览">
              </div>
            </div>
            
            <div class="form-group">
              <label for="site-favicon">网站Favicon URL</label>
              <input 
                type="url" 
                id="site-favicon" 
                v-model="settings.siteFavicon" 
                placeholder="输入Favicon图片URL"
              >
              <div v-if="settings.siteFavicon" class="image-preview small">
                <img :src="settings.siteFavicon" alt="网站Favicon预览">
              </div>
            </div>
          </div>
          
          <div class="form-group">
            <label for="footer-text">页脚文本</label>
            <input 
              type="text" 
              id="footer-text" 
              v-model="settings.footerText" 
              placeholder="输入页脚显示的文本"
            >
          </div>
        </div>
      </div>
      
      <!-- SEO设置 -->
      <div v-if="activeTab === 'seo'" class="settings-section">
        <div class="section-header">
          <h2>SEO设置</h2>
          <p>优化网站在搜索引擎中的表现</p>
        </div>
        
        <div class="settings-form">
          <div class="form-group">
            <label for="meta-keywords">Meta关键词</label>
            <input 
              type="text" 
              id="meta-keywords" 
              v-model="settings.metaKeywords" 
              placeholder="用逗号分隔关键词"
            >
            <small>用逗号分隔关键词，例如：博客,编程,开发,Java</small>
          </div>
          
          <div class="form-group">
            <label for="meta-description">Meta描述</label>
            <textarea 
              id="meta-description" 
              v-model="settings.metaDescription" 
              rows="3"
              placeholder="搜索引擎显示的网站描述"
            ></textarea>
            <small>理想长度在50-160个字符之间</small>
          </div>
          
          <div class="form-group">
            <label for="google-analytics">Google Analytics 代码</label>
            <input 
              type="text" 
              id="google-analytics" 
              v-model="settings.googleAnalytics" 
              placeholder="例如：G-XXXXXXXXXX"
            >
          </div>
          
          <div class="form-group">
            <label>启用百度自动推送</label>
            <div class="toggle-switch">
              <input 
                type="checkbox" 
                id="baidu-push" 
                v-model="settings.baiduPush"
              >
              <label for="baidu-push" class="toggle-label"></label>
            </div>
          </div>
          
          <div class="form-group">
            <label>启用 robots.txt</label>
            <div class="toggle-switch">
              <input 
                type="checkbox" 
                id="enable-robots" 
                v-model="settings.enableRobots"
              >
              <label for="enable-robots" class="toggle-label"></label>
            </div>
          </div>
          
          <div class="form-group" v-if="settings.enableRobots">
            <label for="robots-content">robots.txt 内容</label>
            <textarea 
              id="robots-content" 
              v-model="settings.robotsContent" 
              rows="5"
              class="code-textarea"
            ></textarea>
          </div>
        </div>
      </div>
      
      <!-- 社交媒体设置 -->
      <div v-if="activeTab === 'social'" class="settings-section">
        <div class="section-header">
          <h2>社交媒体设置</h2>
          <p>设置您的社交媒体链接</p>
        </div>
        
        <div class="settings-form">
          <div class="form-group">
            <label for="github-url">
              <i class="fab fa-github"></i> GitHub
            </label>
            <input 
              type="url" 
              id="github-url" 
              v-model="settings.socialLinks.github" 
              placeholder="https://github.com/yourusername"
            >
          </div>
          
          <div class="form-group">
            <label for="weibo-url">
              <i class="fab fa-weibo"></i> 微博
            </label>
            <input 
              type="url" 
              id="weibo-url" 
              v-model="settings.socialLinks.weibo" 
              placeholder="https://weibo.com/yourusername"
            >
          </div>
          
          <div class="form-group">
            <label for="zhihu-url">
              <i class="fab fa-zhihu"></i> 知乎
            </label>
            <input 
              type="url" 
              id="zhihu-url" 
              v-model="settings.socialLinks.zhihu" 
              placeholder="https://www.zhihu.com/people/yourusername"
            >
          </div>
          
          <div class="form-group">
            <label for="linkedin-url">
              <i class="fab fa-linkedin"></i> LinkedIn
            </label>
            <input 
              type="url" 
              id="linkedin-url" 
              v-model="settings.socialLinks.linkedin" 
              placeholder="https://www.linkedin.com/in/yourusername"
            >
          </div>
          
          <div class="form-group">
            <label for="email-contact">
              <i class="fas fa-envelope"></i> 电子邮件
            </label>
            <input 
              type="email" 
              id="email-contact" 
              v-model="settings.socialLinks.email" 
              placeholder="your@email.com"
            >
          </div>
        </div>
      </div>
      
      <!-- 评论设置 -->
      <div v-if="activeTab === 'comments'" class="settings-section">
        <div class="section-header">
          <h2>评论设置</h2>
          <p>配置网站的评论系统</p>
        </div>
        
        <div class="settings-form">
          <div class="form-group">
            <label>启用评论功能</label>
            <div class="toggle-switch">
              <input 
                type="checkbox" 
                id="enable-comments" 
                v-model="settings.enableComments"
              >
              <label for="enable-comments" class="toggle-label"></label>
            </div>
          </div>
          
          <div v-if="settings.enableComments">
            <div class="form-group">
              <label>评论需要审核</label>
              <div class="toggle-switch">
                <input 
                  type="checkbox" 
                  id="comments-moderation" 
                  v-model="settings.commentsRequireModeration"
                >
                <label for="comments-moderation" class="toggle-label"></label>
              </div>
            </div>
            
            <div class="form-group">
              <label for="comment-system">评论系统</label>
              <select id="comment-system" v-model="settings.commentSystem">
                <option value="native">原生评论系统</option>
                <option value="disqus">Disqus</option>
                <option value="valine">Valine</option>
                <option value="gitalk">Gitalk</option>
              </select>
            </div>
            
            <div v-if="settings.commentSystem === 'disqus'" class="form-group">
              <label for="disqus-shortname">Disqus Shortname</label>
              <input 
                type="text" 
                id="disqus-shortname" 
                v-model="settings.disqusShortname" 
                placeholder="your-disqus-shortname"
              >
            </div>
            
            <div v-if="settings.commentSystem === 'valine'" class="form-group">
              <label for="valine-appid">Valine AppID</label>
              <input 
                type="text" 
                id="valine-appid" 
                v-model="settings.valineAppId" 
                placeholder="Valine AppID"
              >
              
              <label for="valine-appkey" class="mt-3">Valine AppKey</label>
              <input 
                type="text" 
                id="valine-appkey" 
                v-model="settings.valineAppKey" 
                placeholder="Valine AppKey"
              >
            </div>
          </div>
        </div>
      </div>
      
      <!-- 首页轮播设置 -->
      <div v-if="activeTab === 'homepage'" class="settings-section">
        <div class="section-header">
          <h2>首页设置</h2>
          <p>配置首页显示内容</p>
        </div>
        
        <div class="settings-form">
          <div class="form-group">
            <label for="hero-title">英雄区标题</label>
            <input 
              type="text" 
              id="hero-title" 
              v-model="settings.heroTitle" 
              placeholder="输入首页英雄区标题"
            >
          </div>
          
          <div class="form-group">
            <label for="hero-subtitle">英雄区副标题</label>
            <input 
              type="text" 
              id="hero-subtitle" 
              v-model="settings.heroSubtitle" 
              placeholder="输入首页英雄区副标题"
            >
          </div>
          
          <div class="form-group">
            <label for="hero-image">英雄区背景图片</label>
            <input 
              type="url" 
              id="hero-image" 
              v-model="settings.heroImage" 
              placeholder="输入背景图片URL"
            >
            <div v-if="settings.heroImage" class="image-preview">
              <img :src="settings.heroImage" alt="英雄区背景预览">
            </div>
          </div>
          
          <div class="form-group">
            <label>显示最新文章</label>
            <div class="toggle-switch">
              <input 
                type="checkbox" 
                id="show-latest-posts" 
                v-model="settings.showLatestPosts"
              >
              <label for="show-latest-posts" class="toggle-label"></label>
            </div>
          </div>
          
          <div class="form-group" v-if="settings.showLatestPosts">
            <label for="latest-posts-count">显示文章数量</label>
            <input 
              type="number" 
              id="latest-posts-count" 
              v-model="settings.latestPostsCount" 
              min="1" 
              max="10"
            >
          </div>
          
          <div class="form-group">
            <label>显示精选项目</label>
            <div class="toggle-switch">
              <input 
                type="checkbox" 
                id="show-featured-projects" 
                v-model="settings.showFeaturedProjects"
              >
              <label for="show-featured-projects" class="toggle-label"></label>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div class="settings-footer">
      <button class="discard-btn secondary-btn" @click="resetSettings">
        <i class="fas fa-undo"></i> 放弃更改
      </button>
      <button class="save-btn primary-btn" @click="saveSettings" :disabled="isSaving">
        <i class="fas fa-save"></i> 保存设置
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SiteSettings',
  data() {
    return {
      isSaving: false,
      activeTab: 'basic',
      tabs: [
        { id: 'basic', name: '基本设置', icon: 'fas fa-cog' },
        { id: 'seo', name: 'SEO设置', icon: 'fas fa-search' },
        { id: 'social', name: '社交媒体', icon: 'fas fa-share-alt' },
        { id: 'comments', name: '评论设置', icon: 'fas fa-comments' },
        { id: 'homepage', name: '首页设置', icon: 'fas fa-home' }
      ],
      settings: {
        // 基本设置
        siteTitle: '个人博客与作品集',
        siteDescription: '专注于分享Java后端技术和经验的个人博客',
        siteLogo: '',
        siteFavicon: '',
        footerText: '© 2024 个人博客与作品集 - 保留所有权利',
        
        // SEO设置
        metaKeywords: '博客,Java,编程,后端开发,项目展示',
        metaDescription: '个人博客与作品集，分享Java后端开发技术、项目实践和编程经验。',
        googleAnalytics: '',
        baiduPush: false,
        enableRobots: true,
        robotsContent: 'User-agent: *\nAllow: /\nDisallow: /admin/',
        
        // 社交媒体设置
        socialLinks: {
          github: 'https://github.com/yourusername',
          weibo: '',
          zhihu: '',
          linkedin: '',
          email: 'your@email.com'
        },
        
        // 评论设置
        enableComments: true,
        commentsRequireModeration: true,
        commentSystem: 'native',
        disqusShortname: '',
        valineAppId: '',
        valineAppKey: '',
        
        // 首页设置
        heroTitle: '资深Java工程师',
        heroSubtitle: '分享编程经验，构建高效服务',
        heroImage: '',
        showLatestPosts: true,
        latestPostsCount: 3,
        showFeaturedProjects: true
      },
      originalSettings: {}
    };
  },
  mounted() {
    this.fetchSettings();
  },
  methods: {
    // 获取设置
    async fetchSettings() {
      try {
        // 这里替换为实际API调用
        // const response = await settingsService.getAll();
        // this.settings = response.data;
        
        // 保存原始设置，用于重置
        this.originalSettings = JSON.parse(JSON.stringify(this.settings));
      } catch (error) {
        console.error('获取设置失败:', error);
        this.$message && this.$message.error('获取设置失败');
      }
    },
    
    // 保存设置
    async saveSettings() {
      try {
        this.isSaving = true;
        
        // 这里替换为实际API调用
        // await settingsService.update(this.settings);
        
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000));
        
        // 保存成功后更新原始设置
        this.originalSettings = JSON.parse(JSON.stringify(this.settings));
        
        alert('设置已保存');
      } catch (error) {
        console.error('保存设置失败:', error);
        alert('保存设置失败');
      } finally {
        this.isSaving = false;
      }
    },
    
    // 重置设置
    resetSettings() {
      if (confirm('确定要放弃所有更改吗？')) {
        this.settings = JSON.parse(JSON.stringify(this.originalSettings));
      }
    }
  }
};
</script>

<style scoped>
.site-settings {
  padding: 20px 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 24px;
  font-weight: 700;
  color: var(--dark-text);
  margin: 0;
}

.primary-btn {
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 10px rgba(67, 97, 238, 0.2);
}

.primary-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(67, 97, 238, 0.3);
}

.primary-btn:disabled {
  background: #aaa;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.secondary-btn {
  background: #f5f7fb;
  color: var(--dark-text);
  border: 1px solid #e0e0e0;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.secondary-btn:hover {
  background: #e9ecef;
}

/* 选项卡样式 */
.settings-tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 25px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 15px;
  overflow-x: auto;
}

.tab-btn {
  padding: 10px 15px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: #666;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  border-radius: 8px;
  transition: all 0.2s;
  white-space: nowrap;
}

.tab-btn:hover {
  background: #f5f7fb;
  color: var(--primary-color);
}

.tab-btn.active {
  background: var(--primary-color);
  color: white;
}

/* 内容区域样式 */
.settings-content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  margin-bottom: 30px;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.settings-section {
  padding: 25px;
}

.section-header {
  margin-bottom: 25px;
}

.section-header h2 {
  font-size: 20px;
  font-weight: 700;
  color: var(--dark-text);
  margin: 0 0 8px 0;
}

.section-header p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 表单样式 */
.settings-form {
  max-width: 800px;
}

.form-group {
  margin-bottom: 20px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--dark-text);
}

.required {
  color: var(--danger-color);
}

small {
  display: block;
  font-size: 12px;
  color: #666;
  margin-top: 5px;
}

.mt-3 {
  margin-top: 15px;
}

input, select, textarea {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1);
}

.code-textarea {
  font-family: monospace;
  background: #f5f7fb;
}

textarea {
  resize: vertical;
  min-height: 80px;
}

.image-preview {
  margin-top: 10px;
  border-radius: 8px;
  overflow: hidden;
  max-height: 200px;
  background: #f5f7fb;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}

.image-preview.small {
  max-height: 100px;
}

.image-preview img {
  max-width: 100%;
  max-height: 180px;
  object-fit: contain;
}

.image-preview.small img {
  max-height: 80px;
}

/* 切换开关样式 */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 22px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-label {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 22px;
}

.toggle-label:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .toggle-label {
  background-color: var(--primary-color);
}

input:checked + .toggle-label:before {
  transform: translateX(22px);
}

/* 页脚样式 */
.settings-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  padding: 20px 0;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .settings-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 10px;
  }
  
  .tab-btn {
    padding: 8px 12px;
    font-size: 13px;
  }
  
  .settings-section {
    padding: 20px 15px;
  }
  
  .form-row {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .settings-footer {
    flex-direction: column-reverse;
    gap: 10px;
  }
  
  .settings-footer button {
    width: 100%;
  }
}
</style> 